package com.gsmr.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gsmr.entity.AlarmThreshold;
import com.gsmr.entity.PageBean;
import com.gsmr.entity.Serverinfo;
import com.gsmr.service.AlarmThresholdService;
import com.gsmr.service.ServerStateService;
import com.gsmr.util.PropertityUtil;
import com.gsmr.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author 作者 : 黄志新
 * @version 创建时间：2017年8月11日 上午12:13:57 
 * 类说明：告警门限Controller层
 */
@Controller
@RequestMapping("/alarmThreshold")
public class AlarmThresholdController {

	@Resource
	private AlarmThresholdService alarmThresholdService;
	
	@Resource
	private ServerStateService serverStateService;

	/**
	 * 分页条件查询告警门限
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows, AlarmThreshold s_alarmThreshold, HttpServletResponse response)
			throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<AlarmThreshold> alarmThresholdList = alarmThresholdService.find(map);
		for (AlarmThreshold alarmThreshold : alarmThresholdList) {
			Map<String, Object> m = new HashMap<>();
			m.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
			m.put("frontProcessorId", alarmThreshold.getFrontProcessorId());
			String siteName = alarmThresholdService.getSiteName(m);
			if (siteName != null) {
				alarmThreshold.setSiteName(siteName);
			}
		}
		Long total = alarmThresholdService.getTotal(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(alarmThresholdList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 服务器告警门限
	 */
	@RequestMapping("/serverList")
	public String serverList(HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		map.put("start", 0);
		map.put("size", 1);
		List<AlarmThreshold> alarmThresholdList = alarmThresholdService.find(map);
		
		Long total = alarmThresholdService.getTotal(map);
		
		map.put("serverId", alarmThresholdList.get(0).getServerId());
		List<Serverinfo> serverinfo = serverStateService.getServer(map);
		if (serverinfo != null && serverinfo.size() > 0)
			alarmThresholdList.get(0).setSiteName(serverinfo.get(0).getServerName() + "空口服务器");
		
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(alarmThresholdList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 添加或者修告警门限
	 * 
	 * @param alarmThreshold
	 * @param response
	 * @param flag
	 *            1 添加 2修改
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(AlarmThreshold alarmThreshold, HttpServletResponse response, Integer flag) throws Exception {
		System.out.println("IN sava");
		
		int resultTotal = 0; // 操作的记录条数

		System.out.println("alarmThreshold = " + alarmThreshold);
		
		if (flag == 1) {
		} else {
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
			map.put("frontProcessorId",alarmThreshold.getFrontProcessorId());
			
			System.out.print("frontProcessorId = " + alarmThreshold.getFrontProcessorId());
			
			map.put("serverId",alarmThreshold.getServerId());
			map.put("cpuUsageThreshold",alarmThreshold.getCpuUsageThreshold());
			map.put("memoryUsageThreshold",alarmThreshold.getMemoryUsageThreshold());
			
			System.out.print("memoryUsageThreshold = " + alarmThreshold.getMemoryUsageThreshold());
			
			map.put("harddiskUsageThreshold",alarmThreshold.getHarddiskUsageThreshold());
			map.put("frontProcessorCpuThreshold",alarmThreshold.getFrontProcessorCpuThreshold());
			map.put("frontProcessorMemoryThreshold",alarmThreshold.getFrontProcessorMemoryThreshold());
			map.put("frontProcessorHardDiskThreshold",alarmThreshold.getFrontProcessorHardDiskThreshold());
			//map.put("packetLossAlarmThreshold",alarmThreshold.getPacketLossAlarmThreshold());
			resultTotal = alarmThresholdService.updateByServerId(map);
			System.out.println(map);
		}
		JSONObject result = new JSONObject();

		Map<String, Object> record = new HashMap<>();

		record.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		record.put("operateTime", new Date().getTime());
		record.put("userId", 1);
		record.put("operationType", 13);
		record.put("oldValue", null);
		record.put("newValue", "CpuUsageThreshold: " + alarmThreshold.getCpuUsageThreshold() + ", MemoryUsageThreshold: "
						+ alarmThreshold.getMemoryUsageThreshold() + ", HarddiskUsageThreshold: "
						+ alarmThreshold.getHarddiskUsageThreshold());

		if (resultTotal > 0) {
			record.put("isImplemented", 1);
			record.put("isSuccessful", 1);
			alarmThresholdService.insertOperateRecord(record);
			result.put("success", true);
		} else {
			record.put("isImplemented", 0);
			record.put("isSuccessful", 0);
			alarmThresholdService.insertOperateRecord(record);
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}

	@Deprecated
	@RequestMapping("/saveFront")
	public String saveFront(AlarmThreshold alarmThreshold, HttpServletResponse response, Integer flag) throws Exception {
		
		int resultTotal = 0; // 操作的记录条数
		System.out.println("flag = " + flag);
		System.out.println(alarmThreshold);
		if (flag == 1) {
		} else {
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
			map.put("frontProcessorId",alarmThreshold.getFrontProcessorId());
			
			map.put("serverId",alarmThreshold.getServerId());
			map.put("cpuUsageThreshold",alarmThreshold.getCpuUsageThreshold());
			map.put("memoryUsageThreshold",alarmThreshold.getMemoryUsageThreshold());
			
			map.put("harddiskUsageThreshold",alarmThreshold.getHarddiskUsageThreshold());
			map.put("frontProcessorCpuThreshold",alarmThreshold.getFrontProcessorCpuThreshold());
			map.put("frontProcessorMemoryThreshold",alarmThreshold.getFrontProcessorMemoryThreshold());
			map.put("frontProcessorHardDiskThreshold",alarmThreshold.getFrontProcessorHardDiskThreshold());
			//map.put("packetLossAlarmThreshold",alarmThreshold.getPacketLossAlarmThreshold());
			//resultTotal = alarmThresholdService.updateByServerId(map);
			resultTotal = alarmThresholdService.updateByfrontProcessorId(map);
		}
		JSONObject result = new JSONObject();
		if (resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}

	@RequestMapping("/saveFrontAlarm")
	public String saveFront(Integer frontProcessorId, Double frontProcessorCpuThreshold, Double frontProcessorMemoryThreshold, Double frontProcessorHardDiskThreshold, HttpServletResponse response, Integer flag) throws Exception {

		int resultTotal = 0; // 操作的记录条数
		System.out.println("flag = " + flag);
		if (flag == 1) {
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
			map.put("frontProcessorId", frontProcessorId);
			map.put("frontProcessorCpuThreshold", frontProcessorCpuThreshold);
			map.put("frontProcessorMemoryThreshold", frontProcessorMemoryThreshold);
			map.put("frontProcessorHardDiskThreshold", frontProcessorHardDiskThreshold);
			System.out.println(map);
			resultTotal = alarmThresholdService.updateByfrontProcessorId(map);
		}
		JSONObject result = new JSONObject();

		Map<String, Object> record = new HashMap<>();

		record.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		record.put("operateTime", new Date().getTime());
		record.put("userId", 1);
		record.put("operationType", 14);
		record.put("oldValue", null);
		record.put("newValue", "frontProcessorId: " + frontProcessorId + ", frontProcessorCpuThreshold: " + frontProcessorCpuThreshold + ", frontProcessorHardDiskThreshold: " + frontProcessorHardDiskThreshold);

		if (resultTotal > 0) {
			record.put("isImplemented", 1);
			record.put("isSuccessful", 1);
			alarmThresholdService.insertOperateRecord(record);
			result.put("success", true);
		} else {
			record.put("isImplemented", 0);
			record.put("isSuccessful", 0);
			alarmThresholdService.insertOperateRecord(record);
			result.put("success", false);
		}

		ResponseUtil.write(response, result);
		return null;
	}
}

